package org.example.controller;

import org.example.entity.CustomerInfo;
import org.example.service.CustomerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息控制器
 */
@RestController
@RequestMapping("/api/customer-info")
public class CustomerInfoController {
    
    @Autowired
    private CustomerInfoService customerInfoService;
    
    /**
     * 获取所有用户信息
     */
    @GetMapping("/list")
    public List<CustomerInfo> getAllCustomers() {
        return customerInfoService.list();
    }
    
    /**
     * 根据ID获取用户信息
     */
    @GetMapping("/{id}")
    public CustomerInfo getCustomerById(@PathVariable Long id) {
        return customerInfoService.getById(id);
    }
    
    /**
     * 根据姓名查询用户信息
     */
    @GetMapping("/search/name")
    public List<CustomerInfo> getCustomersByName(@RequestParam String plName) {
        return customerInfoService.getByPlName(plName);
    }
    
    /**
     * 根据手机号查询用户信息
     */
    @GetMapping("/search/phone")
    public List<CustomerInfo> getCustomersByPhone(@RequestParam String plPhone) {
        return customerInfoService.getByPlPhone(plPhone);
    }
    
    /**
     * 根据身份证号查询用户信息
     */
    @GetMapping("/search/idno")
    public CustomerInfo getCustomerByIdNo(@RequestParam String plIdNo) {
        return customerInfoService.getByPlIdNo(plIdNo);
    }
    
    /**
     * 根据城市查询用户信息
     */
    @GetMapping("/search/city")
    public List<CustomerInfo> getCustomersByCity(@RequestParam String cityName) {
        return customerInfoService.getByCity(cityName);
    }
    
    /**
     * 根据年龄范围查询用户信息
     */
    @GetMapping("/search/age")
    public List<CustomerInfo> getCustomersByAgeRange(
            @RequestParam(required = false) Integer minAge,
            @RequestParam(required = false) Integer maxAge) {
        return customerInfoService.getByAgeRange(minAge, maxAge);
    }
    
    /**
     * 分页查询用户信息
     */
    @GetMapping("/page")
    public List<CustomerInfo> getCustomersPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        // 这里可以使用MyBatis-Plus的分页功能
        return customerInfoService.list();
    }
    
    /**
     * 创建用户信息
     */
    @PostMapping
    public boolean createCustomer(@RequestBody CustomerInfo customerInfo) {
        return customerInfoService.save(customerInfo);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping
    public boolean updateCustomer(@RequestBody CustomerInfo customerInfo) {
        return customerInfoService.updateById(customerInfo);
    }
    
    /**
     * 删除用户信息
     */
    @DeleteMapping("/{id}")
    public boolean deleteCustomer(@PathVariable Long id) {
        return customerInfoService.removeById(id);
    }
}
